package com.tongfusoft.document.web.controller.administration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.MetadataDefine;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.dictionary.service.IDocumentDictionaryService;
import com.tongfusoft.document.manager.metadata.service.IMetadataService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@Controller
public class MetadataController extends BaseController {
	
	@Autowired
	@Qualifier("metaDataManagerService")
	public IMetadataService metaDataManagerService;
	@Autowired
	@Qualifier("documentDictionaryService")
	public IDocumentDictionaryService dictionaryService;

	@RequestMapping("/manager/metadata")
	@UrlDesc(value = "元数据列表页")
	public String metadata(HttpServletRequest request, Model model) {
		
		model.addAttribute("mainPageKey", "metadata");
		model.addAttribute("mainPage", "manager/metadata/metadata");

		return "base";
	}
	
	@RequestMapping("/metadata/query")
	@UrlDesc(value = "元数据查询")
	@ResponseBody
	public DataTable<MetadataDefine> metadataDefineQuery(Model model, int length, int start, int draw, String extra_search) {

		DataTable rm = new DataTable();
		try {
			MetadataDefine define = (MetadataDefine) JsonUtils.getObject4JsonString(extra_search, MetadataDefine.class);
			int pageIndex = (start / length)+1;
			PageInfo pageInfo = this.metaDataManagerService.metadataList(length, pageIndex, define);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {

			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/manager/metadata/addMetadata")
	@UrlDesc(value = "元数据添加页")
	public String addMetadata(HttpServletRequest request, Model model) {
		
		model.addAttribute("mainPageKey", "addMetadata");
		model.addAttribute("mainPage", "manager/metadata/addMetadata");

		return "base";
	}
	
	@RequestMapping("/manager/metadata/metadataDetail/{metadataId}")
	@UrlDesc(value = "元数据详情页")
	public String detail(@PathVariable("metadataId") String metadataId, Model model) {
		
		model.addAttribute("metadataId", metadataId);
		model.addAttribute("mainPageKey", "metadataDetail");
		model.addAttribute("mainPage", "manager/metadata/metadataDetail");
		return "base";
	}
	
	@RequestMapping("/metadata/del")
	@UrlDesc(value = "删除元数据")
	@ResponseBody
	public ResultModel del(MetadataDefine define) {
		
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.metaDataManagerService.deleteMetadata(define, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/manager/metadata/updateMetadata/{metadataId}")
	@UrlDesc(value = "元数据修改页")
	public String updateMetadata(@PathVariable("metadataId") String metadataId, Model model) {
		
		model.addAttribute("metadataId", metadataId);
		model.addAttribute("mainPageKey", "updateMetadata");
		model.addAttribute("mainPage", "manager/metadata/updateMetadata");

		return "base";
	}
	
	@RequestMapping("/metadata/isEnable")
	@UrlDesc(value = "启用和关闭模板")
	@ResponseBody
	public ResultModel switchIsEnable(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			String metadataId = request.getParameter("metadataId");
			String isEnable = request.getParameter("isEnable");
			
			MetadataDefine define = new MetadataDefine();
			define.setMetadataId(metadataId);
			
			this.metaDataManagerService.updateIsEnable(define, isEnable,(Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage()); 
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/metadata/detailData/{metadataId}")
	@UrlDesc(value = "获得元数据详情数据")
	@ResponseBody
	public ResultModel viewMetadataDetail(@PathVariable("metadataId") String metadataId) {
		ResultModel rm = new ResultModel();
		rm.setState("success");	
		
		try {
			MetadataDefine define = new MetadataDefine();
			define.setMetadataId(metadataId);
			MetadataDefine detail = this.metaDataManagerService.getMetadata(define);
			rm.setData(detail);

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("error");
		}

		return rm;

	}
	
	@RequestMapping("/metadata/addData")
	@UrlDesc(value = "添加元数据")
	@ResponseBody
	public ResultModel add(MetadataDefine metadataDefine) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.metaDataManagerService.createMetadata(metadataDefine, (Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/metadata/typeList")
	@UrlDesc(value = "获得元数据类型")
	@ResponseBody
	public ResultModel typeList() {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			rm.setData(this.dictionaryService.getDictByCode("metadaType"));
			
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/metadata/updateData")
	@UrlDesc(value = "修改元数据")
	@ResponseBody
	public ResultModel updateMetadataData(MetadataDefine metadataDefine) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			String metadataId = metadataDefine.getMetadataId();
			
			this.metaDataManagerService.updateMetadata(metadataDefine,metadataId ,(Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/getSelectValue/{metadataId}")
	@UrlDesc(value = "获取下拉框的值")
	@ResponseBody
	public ResultModel getSelectValue(@PathVariable("metadataId") String metadataId) {
		ResultModel rm = new ResultModel();
		rm.setState("success");	
		
		try {
			MetadataDefine define = new MetadataDefine();
			define.setMetadataId(metadataId);
			MetadataDefine detail = this.metaDataManagerService.getMetadata(define);
			String selectData = detail.getSelectData();
			String[] selectDataList =  selectData.split(",");
			rm.setData(selectDataList);

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("error");
		}

		return rm;

	}
	
	
	
	
	
		
	
}
